package main.app;

import graphic.LoginInterface;
import graphic.ManagerInterface;

import java.sql.Connection;

import javax.swing.JTable;

public class LibraryManager implements Runnable {

	private String dbname = "";
	private String dbport = "";
	private String username = "";
	private String userpasswd = "";

	private JTable libro = null;
	private JTable autore = null;
	private JTable posizione_fisica = null;
	private JTable posizione_occupata = null;
	
	private Connection dbConn;	
	private LoginInterface ui;
	private boolean goodCredentials = false;
	
	public void run() {
		ui = new LoginInterface(this);
		ui.setVisible(true);
		while(!goodCredentials)
			try {
				Thread.currentThread().sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		ManagerInterface mi = new ManagerInterface(dbConn, this);
		mi.setVisible(true);
		ui.setVisible(false);
	}
		
	public void setCredentials(String dbnam, String dbpor, String usrnam, String usrpass) {
		dbname = dbnam;
		dbport = dbpor;
		username = usrnam;
		userpasswd = usrpass;
		System.out.println("dbName = " + dbname + ", dbport = " + dbpor + ", username = " + username + ", userpassword = " + userpasswd);
	}
	
	public void setConnection(Connection conn) {
		dbConn = conn;
	}
	
	public void setState(boolean bool) {
		goodCredentials = bool;
	}
	
	public void setJTable(String target, JTable table) {
		if (target.equalsIgnoreCase("libro")) {
			libro = table;
//			System.out.println("Table Libro set");
		}
		if (target.equalsIgnoreCase("autore")) {
			autore = table;
//			System.out.println("Table Autore set");
		}
		if (target.equalsIgnoreCase("posizione_fisica")) {
			posizione_fisica = table;
//			System.out.println("Table PF set");
		}
		if (target.equalsIgnoreCase("posizione_occupata")) {
			posizione_occupata = table;
//			System.out.println("Table PO set");
		}
	}
	
	public JTable getJTable(String target) {
		if (target.equalsIgnoreCase("libro")) {
//			System.out.println("Got table Libro");
			return libro;
		}
		else if (target.equalsIgnoreCase("autore")) {
//			System.out.println("Got table Autore");
			return autore;
		}
		else if (target.equalsIgnoreCase("posizione_fisica")) {
//			System.out.println("Got table PF");
			return posizione_fisica;
		}
		else if (target.equalsIgnoreCase("posizione_occupata")) {
//			System.out.println("Got table PO");
			return posizione_occupata;
		}
		else return null;
	}

}